package edu.berkeley.cs160.innovationers.shoppersidekick;

import edu.berkeley.cs160.innovationers.shoppersidekick.database.MyDB;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class RenameList extends Activity implements OnClickListener{
	
	EditText myTitle;
	myApp appInfo;
	MyDB localDatabase;
	
	public void onClick(View view) {
		switch(view.getId()) {
		case (R.id.CancelButton_rename):
			setResult(myApp.noChange,null);
			finish();
			break;
		case (R.id.OKButton_rename):
			Log.v("test1", "1");
			localDatabase.renameList(appInfo.getShoppingList().get(appInfo.getPosition()).getName(), myTitle.getText().toString());
			Log.v("test1", "2");
			appInfo.getShoppingList().get(appInfo.getPosition()).setName(myTitle.getText().toString());
			
			Intent resultIntent=new Intent();
			setResult(myApp.listNameChanged,resultIntent);
			finish();
			break;
		}
	}
	
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.rename);  
        
        localDatabase = new MyDB(this);
        localDatabase.open();
        
        appInfo = (myApp) getApplication();
        
        getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        myTitle=(EditText)findViewById(R.id.NewName_rename);
        setTitle("Change List Name");
        Button myCancelButton=(Button)findViewById(R.id.CancelButton_rename);
        Button myOKButton=(Button)findViewById(R.id.OKButton_rename);
        
        myCancelButton.setOnClickListener(this);
        myOKButton.setOnClickListener(this);

    }
}

